﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BLL;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace DiChanAPI.Controllers
{
    public class KanChaController : ApiController
    {
        #region 勘察列表
        
        [HttpGet]
        [ActionName("GetKanCha")]
        public IEnumerable<Basic_Info> GetKanCha(string name)
        {
            Basic_Info info = new Basic_Info();
            if (name == null || name == "")
            {
                return new Basic_InfoBLL().GetBasicShow(info);
            }
            else
            {
                return new Basic_InfoBLL().GetBasicShow(info).Where(n => n.ProjectNumber.Contains(name));
            }
        }
        #endregion
        // GET api/kancha
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        #region 详情  
        #region 区位状况详情
        [HttpGet]
        [ActionName("GetLocation")]
        public Location GetLocation(int  id)
        {
            SqlParameter[] sql ={
                                    new SqlParameter("@Location_id",id),
                                };
            return new LocationBLL().GetLocaById(sql);
        }

        #endregion
        #region 小区状况详情
        [HttpGet]
        [ActionName("GetVillage")]
        public Village GetVillage(int id)
        {
            SqlParameter[] par ={
                                    new SqlParameter("@Village_Id",id),
                                };
            return new VillageBLL().GetVillageById(par);
        }
        #endregion
        #region 建筑公共部分详情
        [HttpGet]
        [ActionName("GetFloors")]
        public Floors GetFloors(int id)
        {
            SqlParameter[] par ={
                                    new SqlParameter("@Floor_Id",id),
                                };
            return new FloorsBLL().GetFloorsById(par);
        }
        #endregion
        #region 现场照片详情
        [HttpGet]
        public IHttpActionResult GetPhoto(int id) 
        {
            SqlParameter[] par ={
                                    new SqlParameter("@Photo_Id",id),
                                };
           var list=  new PhotoBLL().GetPhotoById(par);
           byte[] bytes = list.Photo_Url;
           MemoryStream ms = new MemoryStream(bytes);
           ms.Position = 0;
           Image img = Image.FromStream(ms);
           ms.Close();
           return Json<dynamic>(new { img = img, list = list });
        }
        #endregion
        #region 室内状况勘察
        [HttpGet]
        [ActionName("GetIndoor")]
        public Indoor GetIndoor(int id)
        {
            SqlParameter[] par ={
                                    new SqlParameter("@Photo_Id",id),
                                };
            return new IndoorBLL().GetIndoorById(par);
        }
        #endregion
        #region  询价记录详情


        [HttpGet]
        [ActionName("GetInquiry")]
        public Inquiry GetInquiry(int id)
        {
            SqlParameter[] par ={
                                    new SqlParameter("@Photo_Id",id),
                                };
            return new InquiryBLL().GetInquiryById(par);
        }
        #endregion

        #endregion
        // GET api/kancha/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/kancha
        public void Post([FromBody]string value)
        {
        }
        #region 勘察
          
        #region ///区位状况勘察
        [HttpPost]
        [ActionName("PostLocation")]
        public bool PostLocation([FromBody]Location loca)
        {
            SqlParameter[] param ={
                                     new SqlParameter("@Surplus_Years",loca.Location_East),
                                     new SqlParameter("@Basic_Quota",loca.Location_West ),
                                     new SqlParameter("@House_Located",loca.Location_South),
                                     new SqlParameter("@House_Purpose",loca.Location_North),
                                     new SqlParameter("@House_Time",loca.ProjectNumber),
                                     new SqlParameter("@Yers_Source",loca.Location_Position),
                                     new SqlParameter("@Presentation_Type",loca.Location_Traffic),
                                     new SqlParameter("@Assessment",loca.Location_Convenient),
                                     new SqlParameter("@The_Department",loca.Location_Humanity),
                                     new SqlParameter("@Survey",loca.Location_Quality),
                                     new SqlParameter("@Evaluation",loca.Location_Community),
                                     new SqlParameter("@Basic_State",loca.Location_Mature),
                                     new SqlParameter("@ProjectNumber",loca.Location_Supporting),
                                     new SqlParameter("@Issue_Time",loca.Location_Facilities),
                                     new SqlParameter("@Basic_CreateTime",loca.Location_Degree),
                                     new SqlParameter("@Loan_Contacts",loca.Location_Explain),
                                 };
            return new LocationBLL().AddLoca(param);
        }
        #endregion
        #region 小区状况勘察
        [HttpPost]
        [ActionName("PostVillage")]
        public bool PostVillage([FromBody]Village village) 
        { 
            SqlParameter [] param ={
                                   new SqlParameter("@Village_CaseName",village.Village_CaseName),
                                   new SqlParameter("@Village_Cause",village.Village_Cause),
                                   new SqlParameter("@Village_Close",village.Village_Close),
                                   new SqlParameter("@Village_Explain",village.Village_Explain),
                                   new SqlParameter("@Village_Green",village.Village_Green),
                                   new SqlParameter("@Village_Parking",village.Village_Parking),
                                   new SqlParameter("@Village_Property",village.Village_Property),
                                   new SqlParameter("@Village_Structure",village.Village_Structure),
                                   new SqlParameter("@Village_Traffic",village.Village_Traffic),
                                   new SqlParameter("@Village_Volume",village.Village_Volume),
                                   new SqlParameter("@ProjectNumber",village.ProjectNumber),
                               };
            return  new VillageBLL().AddVillage(param);
        }
	    #endregion
        #region  建筑公共部分状况勘察
        [HttpPost]
        [ActionName("PostFloors")]
        public bool PostFloors([FromBody]Floors floors)
        {
            var list=new Basic_Info();
            list.Survey_StartTime = floors.Survey_StartTime;
            if(new Basic_InfoBLL().UpdateStartTime(list))
            {
                SqlParameter[] param ={
                                      new SqlParameter("@Floor_Number",floors.Floor_Number),
                                      new SqlParameter("@Floor_BuildType",floors.Floor_BuildType),
                                      new SqlParameter("@Floor_Address",floors.Floor_Address),
                                      new SqlParameter("@Floor_Door",floors.Floor_Door),
                                      new SqlParameter("@Floor_Explain",floors.Floor_Explain),
                                      new SqlParameter("@Floor_Facade",floors.Floor_Facade),
                                      new SqlParameter("@Floor_Gas",floors.Floor_Gas),
                                      new SqlParameter("@Floor_HouseStructure",floors.Floor_HouseStructure),
                                      new SqlParameter("@Floor_Landmark",floors.Floor_Landmark),
                                      new SqlParameter("@Floor_Lift",floors.Floor_Lift),
                                      new SqlParameter("@Floor_LiftNum",floors.Floor_LiftNum),
                                      new SqlParameter("@Floor_Public",floors.Floor_Public),
                                      new SqlParameter("@Floor_Warm",floors.Floor_Warm),
                                      new SqlParameter("@Floor_Water",floors.Floor_Water),
                                      new SqlParameter("@ProjectNumber",floors.ProjectNumber),
                                      };
                return new FloorsBLL().AddFloors(param);

            }
            else
            {
                return false;
            }
           
        }
        #endregion
        #region ///现场照片提供
        [HttpPost]
        [ActionName("PostPhoto")]
        public bool PostPhoto(dynamic obj)
        {
            string strName = Convert.ToString(obj.str);
            Photo photo = Newtonsoft.Json.JsonConvert.DeserializeObject<Photo>(Convert.ToString(obj.list));
            Image img = new Bitmap(strName);
            MemoryStream stream = new MemoryStream();
            img.Save(stream, ImageFormat.Bmp);
            BinaryReader br = new BinaryReader(stream);
            photo.Photo_Url = stream.ToArray();
            stream.Close();
            SqlParameter[] par ={
                                    new SqlParameter("@Photo_Name",photo.Photo_Name),
                                    new SqlParameter("@Photo_Url",photo.Photo_Url),
                                    new SqlParameter("@ProjectNumber",photo.ProjectNumber),
                                    new SqlParameter("@Photo_CreateTime",DateTime.Now),
                                    new SqlParameter ("@Photo_CreatePeole",photo.Photo_CreatePeole)
                                };
            return new PhotoBLL().AddPhoto(par);
        }
        #endregion 
        #region  室内状况勘察
        [HttpPost]
        [ActionName("PostIndoor")]
        public bool PostIndoor([FromBody]Indoor indoor)
        {
            SqlParameter[] par ={
                                    new SqlParameter("@Indoor_Balcony",indoor.Indoor_Balcony), 
                                    new SqlParameter("@Indoor_Bedroom",indoor.Indoor_Bedroom),
                                    new SqlParameter("@Indoor_CreatePeople",indoor.Indoor_CreatePeople),
                                    new SqlParameter("@Indoor_CreateTime",indoor.Indoor_CreateTime),
                                    new SqlParameter("@Indoor_Explain",indoor.Indoor_Explain),
                                    new SqlParameter("@Indoor_Hall",indoor.Indoor_Hall),
                                    new SqlParameter("@Indoor_HallDegree",indoor.Indoor_HallDegree),
                                    new SqlParameter("@Indoor_Kitchen",indoor.Indoor_Kitchen),
                                    new SqlParameter("@Indoor_KitchenDegree",indoor.Indoor_KitchenDegree),
                                    new SqlParameter("@Indoor_Layer",indoor.Indoor_Layer),
                                    new SqlParameter("@Indoor_LayerNum",indoor.Indoor_LayerNum),
                                    new SqlParameter("@Indoor_Orientation",indoor.Indoor_Orientation),
                                    new SqlParameter("@Indoor_Purpose",indoor.Indoor_Purpose),
                                    new SqlParameter("@Indoor_Residence",indoor.Indoor_Residence),
                                    new SqlParameter("@Indoor_RoomDegree",indoor.Indoor_RoomDegree),
                                    new SqlParameter("@Indoor_Terrace",indoor.Indoor_Terrace),
                                    new SqlParameter("@Indoor_Toilet",indoor.Indoor_Toilet),
                                    new SqlParameter("@Indoor_ToiletDegree",indoor.Indoor_ToiletDegree),
                                    new SqlParameter("@Indoor_Yard",indoor.Indoor_Yard),
                                    new SqlParameter("@Kitchen_Ceiling",indoor.Kitchen_Ceiling),
                                    new SqlParameter("@Kitchen_Cooking",indoor.Kitchen_Cooking),
                                    new SqlParameter("@Kitchen_Ground",indoor.Kitchen_Ground),
                                    new SqlParameter("@Kitchen_Wall",indoor.Kitchen_Wall),
                                    new SqlParameter("@KitchenCeiling_Other",indoor.KitchenCeiling_Other),
                                    new SqlParameter("@KitchenCooking_Other",indoor.KitchenCooking_Other),
                                    new SqlParameter("@KitchenGround_Other",indoor.KitchenGround_Other),
                                    new SqlParameter("@KitchenWall_Other",indoor.KitchenWall_Other),
                                    new SqlParameter("@Living_Ceiling",indoor.Living_Ceiling),
                                    new SqlParameter("@Living_Ground",indoor.Living_Ground),
                                    new SqlParameter("@Living_Wall",indoor.Living_Wall),
                                    new SqlParameter("@LivingCeiling_Other",indoor.LivingCeiling_Other),
                                    new SqlParameter("@LivingGround_Other",indoor.LivingGround_Other),
                                    new SqlParameter("@LivingWall_Other",indoor.LivingWall_Other),
                                    new SqlParameter("@Room_Ceiling",indoor.Room_Ceiling),
                                    new SqlParameter("@Room_Ground",indoor.Room_Ground),
                                    new SqlParameter("@Room_Wall",indoor.Room_Wall),
                                    new SqlParameter("@RoomCeiling_Other",indoor.RoomCeiling_Other),
                                    new SqlParameter("@RoomGround_Other",indoor.RoomGround_Other),
                                    new SqlParameter("@RoomWall_Other",indoor.RoomWall_Other),
                                    new SqlParameter("@Toilet_Ceiling",indoor.Toilet_Ceiling),
                                    new SqlParameter("@Toilet_Ground",indoor.Toilet_Ground),
                                    new SqlParameter("@Toilet_Sanitary",indoor.Toilet_Sanitary),
                                    new SqlParameter("@Toilet_Wall",indoor.Toilet_Wall),
                                    new SqlParameter("@ToiletCeiling_Other",indoor.ToiletCeiling_Other),
                                    new SqlParameter("@ToiletGround_Other",indoor.ToiletGround_Other),
                                    new SqlParameter("@ToiletSanitary_Other",indoor.ToiletSanitary_Other),
                                    new SqlParameter("@ToiletWall_Other",indoor.ToiletWall_Other),
                                    new SqlParameter("@ProjectNumber",indoor.ProjectNumber),
                                };
            return new IndoorBLL().AddFloors(par);
        }
        #endregion
        #region 询价记录勘察
        [HttpPost]
        [ActionName("PostInquiry")]
        public bool PostInquiry([FromBody]Inquiry inquiry)
        {
            SqlParameter[] par ={
                                     new SqlParameter("@Inquiry_Content",inquiry.Inquiry_Content),
                                     new SqlParameter("@Inquiry_CreatePeople",inquiry.Inquiry_CreatePeople),
                                     new SqlParameter("@Inquiry_CreateTime",DateTime.Now),
                                     new SqlParameter("@Inquiry_Time",inquiry.Inquiry_Time),
                                     new SqlParameter("@Inquiry_UnitPrice",inquiry.Inquiry_UnitPrice),
                                     new SqlParameter("@ProjectNumber",inquiry.ProjectNumber),
                                 };
            return new InquiryBLL().AddInquiry(par);
        }
        #endregion
        #endregion

        // PUT api/kancha/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/kancha/5
        public void Delete(int id)
        {
        }
    }
}
